package com.xdhl.fu.fi.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xdhl.fu.common.core.web.domain.AjaxResult;
import com.xdhl.fu.common.security.utils.SecurityUtils;
import com.xdhl.fu.fi.service.IFlightDiscountService;
import com.xdhl.fu.flight.info.api.domain.FlightDiscount;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 折扣控制器
 *
 * @author 朱力维
 * @time 2022/6/9 15:28
 */
@RestController
@RequestMapping("/discount")
public class DiscountController {

    @Resource
    private IFlightDiscountService flightDiscountService;

    @PutMapping
    public AjaxResult updateDiscount(@RequestBody FlightDiscount params) {
        LambdaQueryWrapper<FlightDiscount> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(FlightDiscount::getFlightDataId, params.getFlightDataId());
        FlightDiscount flightDiscount = flightDiscountService.getOne(wrapper);
        if (flightDiscount == null) {
            flightDiscount = new FlightDiscount();
        }
        flightDiscount.setFlightDataId(params.getFlightDataId());
        flightDiscount.setHeadDiscount(params.getHeadDiscount());
        flightDiscount.setBusinessDiscount(params.getBusinessDiscount());
        flightDiscount.setEconomyDiscount(params.getEconomyDiscount());
        flightDiscount.setCreateBy(SecurityUtils.getUsername());
        flightDiscount.setUpdateBy(SecurityUtils.getUsername());
        flightDiscountService.saveOrUpdate(flightDiscount);
        return AjaxResult.success();
    }

}
